const sass = require('sass');
const loadGruntTasks = require('load-grunt-tasks');
const data = require('./data.config.js');

module.exports = function (grunt) {

    // 插件配置项通过grunt.initConfig来初始化
    grunt.initConfig({
        sass: {
            options: {
                implementation: sass,
                style: 'expanded'// compressed
            },
            main: {
                files: [{
                    expand: true,
                    cwd: 'src/assets/styles/',
                    src: [
                        '*.scss'
                    ],
                    dest: 'dist/assets/styles/',
                    ext: '.css'
                }]
            }
        },
        clean: {
            main: {
                files: [{
                    src: 'dist/'
                }]
            }
        },
        babel: {
            options: {
                presets: ['@babel/preset-env']
            },
            main: {
                files: [
                    {
                        expand: true,
                        cwd: 'src/assets/scripts/',
                        src: [
                            '*.js'
                        ],
                        dest: 'dist/assets/scripts/'
                    }
                ],
                // files: {
                //     // 'dist/assets/scripts/main.js': 'src/assets/scripts/main.js'
                //     'dist/assets/scripts/worker.js': 'src/assets/scripts/worker.js'
                // }
            }
        },
        uglify: {
            options: {
                mangle: false
            },
            dist: {
                files: [
                    {
                        expand: true,
                        cwd: 'dist/assets/scripts/',
                        src: [
                            '*.js'
                        ],
                        dest: 'dist/assets/scripts/'
                    }
                ]
            }
        },
        concat: {
            // css: {
            //     src: ['css/animate.css', 'css/bootstrap.min.css'],
            //     dest: 'css/main.css'
            // }
            js: {
                src: ['src/assets/scripts/*.js'],
                dest: 'dist/assets/scripts/main.js'
            }
        },
        web_swig: {
            options: {
                swigOptions: {
                    cache: false
                },
                getData: function (tpl) {
                    return data; // 传递参数
                }
            },
            main: {
                files: [
                    {
                        expand: true,
                        cwd: 'src/',
                        src: [
                            '*.html'
                        ],
                        dest: 'dist/'
                    }
                ]
            }
        },
        imagemin: {
            main: {
                options: {
                    optimizationLevel: 1,
                    pngquant: true
                },
                files: [
                    {
                        expand: true,
                        cwd: 'src/assets/images/',
                        src: ['*.{png,jpg,gif,svg}'],
                        dest: 'dist/assets/images/'
                    }
                ]
            }
        },
        copy: {
            main: {
                files: [
                    {
                        expand: true,
                        cwd: 'public/',
                        src: [
                            '**',
                        ],
                        dest: 'dist/'
                    },
                    {
                        expand: true,
                        cwd: 'src/assets/fonts/',
                        src: [
                            '*.{eot,svg,ttf,woff}',
                        ],
                        dest: 'dist/assets/fonts/'
                    }
                ]
            }
        },
        connect: {
            options: {
                port: 9000,
                hostname: '*', //默认就是这个值，可配置为本机某个 IP，localhost 或域名
                livereload: 35729  //声明给 watch 监听的端口
            },
            server: {
                options: {
                    open: true, //自动打开网页 
                    base: [
                        '.',//主目录
                        'dist',
                        "node_modules"
                    ]
                }
            }
        },
        watch: {
            livereload: {
                options: {
                    livereload: '<%= connect.options.livereload %>' //监听前面声明的端口  35729
                },
                files: [
                    'dist/*.html',
                    'dist/assets/scripts/*.js',
                    'dist/assets/styles/*.scss',
                    'dist/assets/images/*.{png,jpg,gif,svg}',
                    'dist/assets/fonts/*.{eot,svg,ttf,woff}',
                ]
            },
            connect: {
                options: {
                    livereload: '<%= connect.options.livereload %>' //监听前面声明的端口  35729
                },
                files: [
                    'src/*.html',
                    'src/assets/scripts/*.js',
                    'src/assets/styles/*.scss',
                    'src/assets/images/*.{png,jpg,gif,svg}',
                    'src/assets/fonts/*.{eot,svg,ttf,woff}',
                ]
            }
        }
    });


    // 插件安装之后，通过grunt.loadNpmTasks启用插件
    // grunt.loadNpmTasks('grunt-contrib-sass');
    // grunt.loadNpmTasks('grunt-contrib-clean');
    loadGruntTasks(grunt);  // 自动加载插件

    grunt.registerTask('compile', 'clone tasks', ['sass', 'babel', 'imagemin', 'copy', 'web_swig']);


    grunt.registerTask('serve', [
        'connect:server',
        'watch'
    ]);
    // register:注册一个task,用来执行你配置的任务
    grunt.registerTask('default', ['sass']);
}